Ext.define('byb.view.system.MenuTreeView', {
	alias : 'widget.menuTreeView',
	extend : 'Ext.tree.Panel',
	requires : [ 'Ext.data.TreeStore' ],
	xtype : 'check-tree',
	rootVisible : true,
	useArrows : true,
	collapsible : true,
	layout : 'fit',
	initComponent : function() {
		Ext.apply(this, {
			store : new Ext.data.TreeStore({
				proxy : {
					type : 'ajax',
					url : byb.Constant.APIRoot + '/menu/getTree'
				},
				root : {
					text : '权限列表',
					expanded : true
				},
			    autoLoad:false 
			})
		});
		this.callParent();
	}
	,listeners:{
		beforeselect:function(ctx,record,index,eOpts ){
			if('BYB' !== byb.controller.SystemController.loginUserOrgCode){
				var el = Ext.query('[role=checkbox]')[index - 1];
				if(el){
					el.disabled = true;
				}
			}
		}
		,checkchange: function( node, check){
			if('BYB' === byb.controller.SystemController.loginUserOrgCode){
				if(node.isLeaf()){
					var parent = node.parentNode;
					var clear = true; 
					
					Ext.Array.each(parent.childNodes, function(n, index) {
						if(n.get('checked')){
							clear = false;
							return false;
						}
					});
					
					if(!check && clear){
						parent.set('checked',false);
					}else{
						parent.set('checked', true);
					}
					
				}else{
					Ext.Array.each(node.childNodes, function(n, index) {
						n.set('checked',check);
					});
				}
    		}
		}
	}
});